package cn.tukk.otherTest;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;
import scala.Tuple2;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @program: sparkProjectNew
 * @description
 * @author: tkk fendoukaoziji  出错了啊 s啥
 * @create: 2019-02-28 14:15
 **/
public class TestMap {
    public static void main(String[] args) {
        // 创建SparkConf
        SparkConf conf = new SparkConf()
                .setAppName("AreaTop3ProductSpark").setMaster("local");
        JavaSparkContext sc = new JavaSparkContext(conf);
        JavaRDD<String>  lines = sc.textFile("wordCount");
        JavaRDD<String> words = lines.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public Iterable<String> call(String s) throws Exception {
                return Arrays.asList(s.split(" "));
            }
        });
        words.foreach(s -> System.out.println(s));
        JavaPairRDD<String, Integer> pairWords = words.mapToPair(new PairFunction<String, String, Integer>() {
            @Override
            public Tuple2<String, Integer> call(String s) throws Exception {
                return new Tuple2<>(s, 1);
            }
        });
        pairWords.foreach(stringIntegerTuple2 -> System.out.println(stringIntegerTuple2));
        JavaPairRDD<String, Integer> result = pairWords.reduceByKey(new Function2<Integer, Integer, Integer>() {
            @Override
            public Integer call(Integer v1, Integer v2) throws Exception {
                return v1 + v2;
            }
        });
        result.foreach(integerTuple2 -> System.out.println(integerTuple2));
        sc.stop();
    }



}






















